package net.zjitc.servlet.log;

import net.zjitc.domain.User;
import net.zjitc.service.user.UserService;
import net.zjitc.service.user.userServiceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by hasee on 2017/10/21.
 */
@WebServlet(name = "LogonServlet",urlPatterns = "/logonServlet")
public class LogonServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkPassword = request.getParameter("checkPassword");
        String phone = request.getParameter("phone");
        String msg = "";
        if("".equals(username)){
            msg = "请填写用户名";
            request.getSession().setAttribute("logonMsg",msg);
            response.sendRedirect("/newweb/log/logon.jsp");
        }else if("".equals(password)) {
            msg = "请填写密码";
            request.getSession().setAttribute("logonMsg",msg);
            response.sendRedirect("/newweb/log/logon.jsp");
        }else if(!password.equals(checkPassword)){
            msg = "两次输入密码不一致";
            request.getSession().setAttribute("logonMsg",msg);
            response.sendRedirect("/newweb/log/logon.jsp");
        } else if("".equals(phone)){
            msg = "请填写手机号码";
            request.getSession().setAttribute("logonMsg",msg);
            response.sendRedirect("/newweb/log/logon.jsp");
        }else {
            UserService service = new UserServiceImpl();
            User user = service.logon(username,password,phone);
            if(user==null){
                msg = "用户名已存在";
                request.getSession().setAttribute("logonMsg",msg);
                response.sendRedirect("/newweb/log/logon.jsp");
            }else {
                msg = "注册成功";
                request.getSession().setAttribute("loginMsg",msg);
                response.sendRedirect("/newweb/log/login.jsp");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
